package com.prj.ufdm.web.config;

import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 配置auth数据源
 * @author Administrator
 */
@Configuration
public class DataSourceConfig {
	
    @Value("${main.ds.driverClassName}")
    private String mainDriverClassName;
    @Value("${main.ds.url}")
    private String mainDsUrl;
    @Value("${main.ds.username}")
    private String mainDsUsername;
    @Value("${main.ds.password}")
    private String mainDsPassword;
    
    @Value("${main.ds.initialSize}")
    private String mainDsInitialSize;
    @Value("${main.ds.minIdle}")
    private String mainDsMinIdle;
    @Value("${main.ds.maxActive}")
    private String mainDsMaxActive;
    @Value("${main.ds.validationQuery}")
    private String mainDsValidationQuery;
    
    @Bean(name = "mainDsDataSource") 
    @Primary
    public DataSource ogjjDataSource() throws SQLException {
        DruidDataSource basicDataSource = new DruidDataSource();
        basicDataSource.setDriverClassName(mainDriverClassName);
        basicDataSource.setUrl(mainDsUrl);
        basicDataSource.setUsername(mainDsUsername);
        basicDataSource.setPassword(mainDsPassword);
		try {
			basicDataSource.setInitialSize(Integer.parseInt(mainDsInitialSize));
		} catch (Exception er) {
			basicDataSource.setInitialSize(1);
		}
		try {
			basicDataSource.setMinIdle(Integer.parseInt(mainDsMinIdle));
		} catch (Exception er) {
			basicDataSource.setMinIdle(1);
		}
		try {
			basicDataSource.setMaxActive(Integer.parseInt(mainDsMaxActive));
		} catch (Exception er) {
			basicDataSource.setMaxActive(10);
		}
        basicDataSource.setFilters("stat");
        basicDataSource.setTestWhileIdle(true);
        basicDataSource.setValidationQuery(mainDsValidationQuery);
        basicDataSource.setTestOnReturn(true);
        basicDataSource.setTestOnBorrow(false);
        basicDataSource.setTimeBetweenEvictionRunsMillis(30000);
        System.out.println("\n mainDs 主数据启动，配置：" + basicDataSource.getUrl() + "; driverClassName="+mainDriverClassName);
        return basicDataSource;
        
    }
}
